﻿Imports System.Data
Imports System.Data.SqlClient

Namespace DB

    ''' <summary>
    ''' TypeName:    FeaturesType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: SQC_Features
    ''' </summary>
    ''' <remarks></remarks>

    Public Class FeaturesType
        Inherits DBTableObjectBase

#Region "Declarations"
        'Protected Friend iFeatureId as Guid
        Protected Friend iEquipmentId As Guid
        Protected Friend iCategory As String
        Protected Friend iSubCategory As String
        Protected Friend iFeatureName As String
        Protected Friend iFeatureValue As String
        Protected Friend iOrderIndex As Integer
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New(TableNamesEnum.SQC_Features)
        End Sub
        Public Sub New(ByVal FeatureId As Guid)
            MyBase.New(TableNamesEnum.SQC_Features, FeatureId)
        End Sub
        Public Sub New(ByVal FeatureId As Guid, _
                        ByVal EquipmentId As Guid, _
                        ByVal Category As String, _
                        ByVal SubCategory As String, _
                        ByVal FeatureName As String, _
                        ByVal FeatureValue As String, _
                        ByVal OrderIndex As Integer, _
                        ByVal LMUserId As Guid, _
                        ByVal LMDate As DateTime)
            MyBase.New(TableNamesEnum.SQC_Features)
            Me.iId = FeatureId
            Me.iEquipmentId = EquipmentId
            Me.iCategory = Category
            Me.iSubCategory = SubCategory
            Me.iFeatureName = FeatureName
            Me.iFeatureValue = FeatureValue
            Me.iOrderIndex = OrderIndex
            Me.iLMUserId = LMUserId
            Me.iLMDate = LMDate

        End Sub
#End Region

#Region "Exposed Properties"
        Public Property FeatureId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Property EquipmentId() As Guid
            Get
                Return Me.iEquipmentId
            End Get
            Set(ByVal value As Guid)
                Me.iEquipmentId = value
            End Set
        End Property
        Public Property Category() As String
            Get
                Return Me.iCategory
            End Get
            Set(ByVal value As String)
                Me.iCategory = value
            End Set
        End Property
        Public Property SubCategory() As String
            Get
                Return Me.iSubCategory
            End Get
            Set(ByVal value As String)
                Me.iSubCategory = value
            End Set
        End Property
        Public Property FeatureName() As String
            Get
                Return Me.iFeatureName
            End Get
            Set(ByVal value As String)
                Me.iFeatureName = value
            End Set
        End Property
        Public Property FeatureValue() As String
            Get
                Return Me.iFeatureValue
            End Get
            Set(ByVal value As String)
                Me.iFeatureValue = value
            End Set
        End Property
        Public Property OrderIndex() As Integer
            Get
                Return Me.iOrderIndex
            End Get
            Set(ByVal value As Integer)
                Me.iOrderIndex = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iId = Guid.Empty Else Me.iId = Row(0)
            If IsDBNull(Row(1)) Then Me.iEquipmentId = Nothing Else Me.iEquipmentId = Row(1)
            If IsDBNull(Row(2)) Then Me.iCategory = Nothing Else Me.iCategory = Row(2)
            If IsDBNull(Row(3)) Then Me.iSubCategory = Nothing Else Me.iSubCategory = Row(3)
            If IsDBNull(Row(4)) Then Me.iFeatureName = Nothing Else Me.iFeatureName = Row(4)
            If IsDBNull(Row(5)) Then Me.iFeatureValue = Nothing Else Me.iFeatureValue = Row(5)
            If IsDBNull(Row(6)) Then Me.iOrderIndex = Nothing Else Me.iOrderIndex = Row(6)
            If IsDBNull(Row(7)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(7)
            If IsDBNull(Row(8)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(8)
            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If Me.iId = Guid.Empty Then
                        .AddWithValue("@FeatureId", System.DBNull.Value)
                    Else
                        .AddWithValue("@FeatureId", Me.iId)
                    End If
                    If Me.iEquipmentId = Guid.Empty Then
                        .AddWithValue("@EquipmentId", System.DBNull.Value)
                    Else
                        .AddWithValue("@EquipmentId", Me.iEquipmentId)
                    End If
                    If Me.iCategory = String.Empty Then
                        .AddWithValue("@Category", System.DBNull.Value)
                    Else
                        .AddWithValue("@Category", Me.iCategory)
                    End If
                    If Me.iSubCategory = String.Empty Then
                        .AddWithValue("@SubCategory", System.DBNull.Value)
                    Else
                        .AddWithValue("@SubCategory", Me.iSubCategory)
                    End If
                    If Me.iFeatureName = String.Empty Then
                        .AddWithValue("@FeatureName", System.DBNull.Value)
                    Else
                        .AddWithValue("@FeatureName", Me.iFeatureName)
                    End If
                    If Me.iFeatureValue = String.Empty Then
                        .AddWithValue("@FeatureValue", System.DBNull.Value)
                    Else
                        .AddWithValue("@FeatureValue", Me.iFeatureValue)
                    End If
                    If Me.iOrderIndex = Nothing Then
                        .AddWithValue("@OrderIndex", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderIndex", Me.iOrderIndex)
                    End If
                    If Me.iLMUserId = Guid.Empty Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If Me.iLMDate = DateTime.MinValue Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If Me.iId = Guid.Empty Then
                        .AddWithValue("@FeatureId", System.DBNull.Value)
                    Else
                        .AddWithValue("@FeatureId", Me.iId)
                    End If
                    If Me.iEquipmentId = Guid.Empty Then
                        .AddWithValue("@EquipmentId", System.DBNull.Value)
                    Else
                        .AddWithValue("@EquipmentId", Me.iEquipmentId)
                    End If
                    If Me.iCategory = String.Empty Then
                        .AddWithValue("@Category", System.DBNull.Value)
                    Else
                        .AddWithValue("@Category", Me.iCategory)
                    End If
                    If Me.iSubCategory = String.Empty Then
                        .AddWithValue("@SubCategory", System.DBNull.Value)
                    Else
                        .AddWithValue("@SubCategory", Me.iSubCategory)
                    End If
                    If Me.iFeatureName = String.Empty Then
                        .AddWithValue("@FeatureName", System.DBNull.Value)
                    Else
                        .AddWithValue("@FeatureName", Me.iFeatureName)
                    End If
                    If Me.iFeatureValue = String.Empty Then
                        .AddWithValue("@FeatureValue", System.DBNull.Value)
                    Else
                        .AddWithValue("@FeatureValue", Me.iFeatureValue)
                    End If
                    If Me.iOrderIndex = Nothing Then
                        .AddWithValue("@OrderIndex", System.DBNull.Value)
                    Else
                        .AddWithValue("@OrderIndex", Me.iOrderIndex)
                    End If
                    If Me.iLMUserId = Guid.Empty Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If Me.iLMDate = DateTime.MinValue Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If
                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Return Me.iId.ToString
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Return Nothing
        End Function
#End Region

#Region "Exposed Implementations"
        Public Function Clone() As FeaturesType
            Return New FeaturesType(Me.iId, Me.iEquipmentId, Me.iCategory, Me.iSubCategory, Me.iFeatureName, Me.iFeatureValue, Me.iOrderIndex, Me.iLMUserId, Me.iLMDate)
        End Function
#End Region

    End Class

    Public Class FeaturesTypeCollection
        Inherits DBTableObjectCollectionBase

        Public DataFilters As New DataFilterTypeCollection(Of FeaturesTypeFields)(Me.iTablename.ToString)
        Public DataSorters As New DataSorterTypeCollection(Of FeaturesTypeFields)(Me.iTableName.ToString)

        Public Sub New()
            MyBase.New(TableNamesEnum.SQC_Features)
        End Sub

        Public Sub New(EquipmentId As Guid)
            MyBase.New(TableNamesEnum.SQC_Features, EquipmentId)
        End Sub

        Default Public Property Item(ByVal Index As Integer) As FeaturesType
            Get
                Return CType(Me.List.Item(Index), FeaturesType)
            End Get
            Set(ByVal value As FeaturesType)
                Me.List.Item(Index) = value
            End Set
        End Property

        Protected Friend Overrides Function Load() As Boolean
            If Me.iRefId = Guid.Empty Then
                Return Me.Load("SELECT * " & _
                               "FROM " & Me.iTableName.ToString & _
                               Me.DataFilters.GetSQL & _
                               Me.DataSorters.GetSQL)
            Else
                Return Me.Load("SELECT * " & _
                               "FROM " & Me.iTableName.ToString & " " & _
                               "WHERE [EquipmentId]='" & Me.iRefId.ToString & "' " & _
                               "ORDER BY [OrderIndex],[Category],[SubCategory],[FeatureName]")
            End If
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            List.Clear()

            'Build new list
            Dim typeObject As FeaturesType

            For Each row In dt.Rows
                typeObject = New FeaturesType()
                typeObject.iConnection = Me.iConnection
                typeObject.iTransaction = Me.iTransaction
                If typeObject.Load(row) = True Then Me.List.Add(typeObject)
            Next
            Return True
        End Function

        Public Overloads Sub Sort(ByVal propertyToSort As FeaturesTypeFields, ByVal sortOrder As SortOrder)
            ipropertyToSort = propertyToSort.ToString
            isortOrder = sortOrder
            Me.InnerList.Sort(Me)
        End Sub

        Public Function Clone() As FeaturesTypeCollection
            Dim nFeatures As New FeaturesTypeCollection

            If Me.List.Count > 0 Then
                For i As Integer = 0 To Me.List.Count - 1
                    Dim f As FeaturesType = Me.List(i)
                    nFeatures.Add(f.Clone)
                Next
            End If

            Return nFeatures
        End Function

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum FeaturesTypeFields
        FeatureId
        EquipmentId
        Category
        SubCategory
        FeatureName
        FeatureValue
        OrderIndex
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace